<?php
include_once('../includes/definer.php');
include_once(INC.'php/bootstrap.php');
$orderColumn = array(
    'p.id',
    'p.titulo',
    'c.value',
    'e.value'
);

$recordsTotal = Doctrine_Query::create()
        ->select('count(p.id)')
        ->from('Publicacion p')
        ->innerJoin('p.categoria c')
        ->innerJoin('p.estado e')
        ->where('e.id <> ?', Estado::BORRADO)
        ->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR)
;
//<a href=\"producto/", p.slug, "\" class=\"btn btn-primary\"><i class=\"fa fa-pencil\"></i></a>
$data = Doctrine_Query::create()
        ->select('
            p.id,
            c.id as catId,
            p.titulo as titulo,
            CONCAT("<a data-publicacion=\"",p.id,"\" onclick=\"addPedidothis\" class=\"btn btn-success btn-xs\"><i class=\"fa fa-plus\"></i></a>") as acciones,
            CONCAT("row", p.id) as DT_RowId,
            c.value as categoria,
            e.value as estado
        ')
        ->from('Publicacion p')
        ->innerJoin('p.categoria c')
        ->innerJoin('p.estado e')
        ->where('e.id <> ?', Estado::BORRADO)
        ->limit($_GET['length'])
        ->offset($_GET['start'])
        ->orderBy($orderColumn[$_GET['order'][0]['column']].' '.$_GET['order'][0]['dir'])
;
//busqueda
if ($_GET['search']['value']) {
    $data->andWhere('p.titulo like "%'.$_GET['search']['value'].'%"');
}
//fin busqueda
$q2 = $data->copy();
$q2->removeDqlQueryPart('select')
        ->removeDqlQueryPart('limit')
        ->removeDqlQueryPart('offset')
        ->groupBy('1=1')
        ->addSelect('count(p.id)')
;
$recordsFiltered = $q2->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR);

//echo(json_encode($productos)); exit();
$restul = array(
    'recordsTotal'=>$recordsTotal,
    'recordsFiltered'=>$recordsFiltered,
    'data'=>$data->execute(array(), Doctrine::HYDRATE_ARRAY)
);
echo(str_replace('addPedidothis', 'addPedido(this)', json_encode($restul)));
?>